﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using com.hebg3.app.model;
using System.Xml;
using com.hebg3.util;
using System.IO;

namespace com.hebg3.app.reg {
  /// <summary>
  /// 安装记录
  /// </summary>
  class InstallReg {
    /// <summary>
    /// 记录安装
    /// </summary>
    /// <param name="app"></param>安装的应用
    /// <param name="userName"></param>用户名
    /// <param name="userNumber"></param>用户编号
    public void regInstall(App app,String userName,String userNumber) {
      //得到安装记录xml
      XmlDocument doc = getInstallRegXml(userName,userNumber);
      //得到安装记录节点
      XmlNode install_reg = doc.SelectSingleNode("/user/install_reg");

      //创建新的安装记录节点
      XmlElement install = doc.CreateElement("install");
      //安装应用id
      install.SetAttribute("installAppId",app.getId());
      //安装应用name
      install.SetAttribute("installAppName", app.getName());
      //安装时间
      DateTime date = DateTime.Now;
      install.SetAttribute("installTime", date.ToString("yyyy-MM-dd HH:mm:ss"));

      install_reg.AppendChild(install);

      doc.Save(SysUtils.getInstallRegFile());
      
    }
    /// <summary>
    /// 得到
    /// </summary>
    /// <param name="userName"></param>
    /// <param name="userNumber"></param>
    /// <returns></returns>
    public XmlDocument getInstallRegXml(String userName, String userNumber) {
      //实例化doc对象
      XmlDocument doc = new XmlDocument();
      String xmlPath = SysUtils.getInstallRegFile();
      if( !File.Exists(xmlPath) ) {
        //加入xml声明段落
        XmlDeclaration xmlDeclaration = doc.CreateXmlDeclaration("1.0", "utf-8", null);
        //加入doc
        doc.AppendChild(xmlDeclaration);

        XmlElement user = doc.CreateElement("user");
        user.SetAttribute("userName", userName);
        user.SetAttribute("userNumber", userNumber);
        doc.AppendChild(user);

        XmlElement install_reg = doc.CreateElement("install_reg");
        user.AppendChild(install_reg);
      } else {
        doc.Load(xmlPath);
      }
      return doc;
    }

  }
}
